#!/bin/sh
# 
# The purpose of this script is to install carblogger_ui as a service.
# This script is executed by update-rc.d 
# 
# See the part on postinst in section 9.3.3.2 Running initscripts at:
# http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit
#
# E.g. for 'postinst': update-rc.d mswitch start 20 2 3 4 5 . stop 0 1 6 .
#
# TODO: See about, "The dh_installinit tool will take care of inserting the
# right update-rc.d calls in postinst and postrm, including making sure the
# maintainer scripts are being called during the appropriate state transitions.
# the paul Oct 11 '12 at 4:33
#

# #START debug
# echo "postrm" > /tmp/prerm.log
# for var in "$@"
# do
#     echo "$var" >> /tmp/prerm.log
# done
# exit 0
# #END: debug


case "$1" in

  configure)
    # Check if zenity is installed and can be run (needs X).
    # This is so I can print a message during install time.
    # The zenity thing has problems as root. I should just
    # prompt on the command line if needed.
    zenity --version 
    
    if [ $? -eq 0 ]; then
        zenity --timeout=1 --notification --text="Configuring the CARBLogger_UI daemon to start at boot time."
        
        if zenity --title="CARBLogger_UI Daemon" --question --text="Do you want to start the CARBLogger service now?"; then
        sleep 1 # Let the log file poplulate a bit.
        
        # Copy carblogger_ui log file to /tmp so I can append an all done message to it.
        cp /var/log/carblogger_ui.log /tmp/_carblogger_ui.log
        echo -e"\nThe CARBLogger_UI Daemon is running. You may now close this window." >> /tmp/_carblogger_ui.log
        zenity --text-info  --filename=/tmp/foo.log
        fi
    else
            echo "Zenity couldn't be started. Continuing without extra dialog."
    fi

    update-rc.d carblogger_runner defaults || true
  ;;

  abort-upgrade|abort-remove|abort-deconfigure)
    exit 0
  ;;

  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 1
  ;;

esac


exit 0
